<?php 
$page_title='Add a URL';
include './includes/header.html';
require_once 'mysql_connect.php';

if(isset($_POST['submitted'])){
	//check for a url
	if (eregi('^([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_\.~?\-]*)$', $_POST['url'])){
		$u=escape_data($_POST['url']);
	}else{
		$u=FALSE;
		echo '<p><font color="red">Please enter a valid URL!</font></p>';
	}
	
	//check for a url title
	if (!empty($_POST['title'])){
		$t=escape_data($_POST['title']);
	}else{
		$t=FALSE;
		echo '<p><font color="red">Please enter a URL name!</font></p>';
	}
	
	//check for a description
	if (!empty($_POST['description'])){
		$d=escape_data($_POST['description']);
	}else {
		$d=FALSE;
		echo '<p><font color="red">Please enter a description!</font></p>';
	}
	
	//check for a category
	if (isset($_POST['types'])&&(is_array($_POST['types']))){
		$type=TRUE;
	}else {
		$type=FALSE;
		echo '<p><font color="red">Please select at least one category !</font></p>';
	}
	
	if($u&&$t&&$d&&$type){
		//add
		$query="INSERT INTO urls (url,title,description) values ('$u','$t','$d')";
		$result=@mysql_query($query);
		$uid=@mysql_insert_id();
		
		if ($uid>0){
			//make the url associations
			$query='INSERT INTO url_associations (url_id,url_category_id,approved) values ';
			foreach ($_POST['types'] as $v){
				$query.="($uid,$v,'Y'), ";
			}
			$query=substr($query, 0,-2);
			$result=@mysql_query($query);
			if (mysql_affected_rows()==count($_POST['types'])){
				echo '<p><b>Thank you for your submission!</b></p>';
				$_POST=array();//reset values
			}else {//second query not ok
				echo '<p><font color="red">your submission could not be processed</font></p>';
				echo '<p><font color="red">'.mysql_errno().'</br></br>Query:'.$query.'</font></p>';
				
				//delete the url
				$query="DELETE FROM urls WHERE url_id=$uid";
				@mysql_query($query);
			}
		}else{//first query not ok
			echo '<p><font color="red">your submission could not be processed</font></p>';
			echo '<p><font color="red">'.mysql_errno().'</br></br>Query:'.$query.'</font></p>';
		}
	}else {//if one of the data test failed
		echo '<p><font color="red">please try again</font></p>';
	}
}
?>
<form action="add_url.php" method="post">
	<fieldset><legend>Fill out the form to submit a URL</legend>
		<p><b>URL:</b><input type="text" name="url" size="60" maxlength="60" value="<?php if (isset
		($_POST['url'])) echo $_POST['url'];?>"/><small>Do not include the initial<i>http://</i></small></p>
		
		<p><b>URL Name:</b><input type="text" name="title" size="60" maxlength="60" value="<?php if (isset
		($_POST['title'])) echo $_POST['title'];?>"/></p>
		
		<p><b>Description:</b><textarea name="description" cols="40" rows="5" ><?php if (isset
		($_POST['description'])) echo $_POST['description'];?></textarea></p>
		
		<p><b>Category:</b><select name="types[]" multiple="multiple" size="5">
		<?php //create the pull down information 
		$query="SELECT * FROM url_categories ORDER BY category ASC";
		$result=@mysql_query($query);
		while ($row=mysql_fetch_array($result)){
			echo "<option value=\"$row[0]\"";
			if (isset($_POST['types'])&&(in_array($row[0], $_POST['types']))){
				echo 'selected="selected"';
			}
			echo ">$row[1]</option>\n";
		}
		?>
		</select></p>
	</fieldset>
	<input type="hidden" name="submitted" value="TRUE"/>
	<div align="center"><input type="submit" name="submit" value="Submit"/></div>
</form>
<?php 
mysql_close();
include './includes/footer.html';
?>


